import axios from 'axios';
import Cookies from 'js-cookie';

const request = axios.create();

request.interceptors.request.use(
  config => {
    const token = Cookies.get('token');
    if (!!token) {
      config.headers!.authorization = token;
    }
    return config;
  },
  error => {
    console.log('>>> request error', error);
    return Promise.reject(error);
  },
);

request.interceptors.response.use(
  response => {
    const { status, data: resData } = response;
    if (status !== 200) {
      return Promise.reject(resData);
    }
    return resData;
  },
  error => {
    console.log('>>> response error', error);
    return Promise.reject(error);
  },
);

export default request;
